package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.entity.Unit;
import com.yqg.manage.mapper.DepartmentMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IDepartmentService;
import com.yqg.manage.entity.Department;

import org.springframework.web.bind.annotation.RestController;

/**
 * DepartmentController 前端控制器
 *
 * @author zxf
 * @since 2025-09-16
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private IDepartmentService departmentService;

    @Resource
    private DepartmentMapper departmentMapper;


    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新科室")
    public Result save(@RequestBody Department department) {
        return Result.success(departmentService.saveOrUpdate(department));
    }

    /**
     * 删除前验证该单位是否有用户
     */
    @GetMapping("deleteBeforeVerify")
    public Result deleteBeforeVerify(@RequestParam Long id) {
        Integer b = departmentMapper.deleteBeforeVerify(id);
        return Result.success(b);
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除科室")
    public Result delete(@PathVariable Integer id) {
        departmentMapper.deleteDepartment(id);
        return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Department> list = departmentService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(departmentService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(departmentService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 查询所有根集合
     */
    @GetMapping("/rootPage")
    public Result rootPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) Long unitId) {
        pageNum = (pageNum - 1) * pageSize;
        Map<String,Object> map = departmentService.rootPage(pageNum,pageSize,unitId);
        return Result.success(map);
    }

    /**
     * 副主任查询负责单位的根节点
     */
    @GetMapping("/deputyDirectorRootPage")
    public Result deputyDirectorRootPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam Long userId,
                           @RequestParam(required = false) Long unitId) {
        pageNum = (pageNum - 1) * pageSize;
        Map<String,Object> map = departmentService.deputyDirectorRootPage(pageNum,pageSize,userId,unitId);
        return Result.success(map);
    }

    /**
     * 查询某部门的子部门
     */
    @GetMapping("/childDepartment")
    public Result childDepartment(@RequestParam Long id) {
        Map<String,Object> map = departmentService.childDepartment(id);
        return Result.success(map);
    }

    /**
     * 根据单位查询部门
     *
     * @param isRoot 是否只要根节点 (1只要根节点，0全部部门)
     * @param unitId 单位ID
     */
    @GetMapping("/unitDepartment")
    public Result unitDepartment(@RequestParam Long unitId,
                                 @RequestParam Byte isRoot) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getIsDelete, "0")
                .eq(Department::getUnitId, unitId)
                .orderByAsc(Department::getId);
        if (isRoot == 1) {
            queryWrapper.isNull(Department::getPid);
        }
        List<Department> list = departmentService.list(queryWrapper);
        return Result.success(list);
    }

}

